package com.acic.jba.db;

import java.io.File;
import java.sql.SQLException;

import org.nutz.dao.Dao;
import org.nutz.dao.impl.NutDao;
import org.nutz.dao.impl.SimpleDataSource;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.lang.Files;

@IocBean
public class DaoFactory {
	public static final int ORACLE = 1;
	public static final int MYSQL = 2;
	public static final int SQLITE = 3;


	public static Dao getDao(int type) {
		SimpleDataSource dataSource = new SimpleDataSource();
		if (type == ORACLE) {
			dataSource.setJdbcUrl("jdbc:oracle:thin:@10.0.0.113:1521:orcl");
			dataSource.setUsername("acba");
			dataSource.setPassword("acba");
		} else if (type == SQLITE) {
			File file = Files.findFile("config/data.db");
			dataSource.setJdbcUrl("jdbc:sqlite:/" + file.getPath().toLowerCase().replace("\\", "/"));
			try {
				dataSource.getConnection().setAutoCommit(true);
			} catch (SQLException e) {
				e.printStackTrace();
			}
		} else {
			dataSource.setJdbcUrl("jdbc:mysql://172.16.32.192:3306/aczy_app");
			dataSource.setUsername("root");
			dataSource.setPassword("1qaz@WSX");
		}
		return new NutDao(dataSource);
	}

	public static Dao getSQLiteDao(File file) {
		SimpleDataSource dataSource = new SimpleDataSource();
		dataSource.setJdbcUrl("jdbc:sqlite:/" + file.getPath().toLowerCase().replace("\\", "/"));
		return new NutDao(dataSource);
	}

}
